package com.xm.biz.impl;

import com.xm.biz.GoodsBiz;
import com.xm.dao.GoodsDao;
import com.xm.dao.impl.GoodsDaoImpl;
import com.xm.po.Goods;
import com.xm.util.PageBean;

import java.util.Date;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-10-18:38
 * @description 商品业务逻辑接口实现类
 */
public class GoodsBizImpl implements GoodsBiz {
    //初始化数据层
    GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public List<Goods> getGoodsByTypeTop6(Integer typeId) {
        return goodsDao.getGoodsByTypeTop6(typeId);
    }

    @Override
    public boolean add(Goods goods) {
        goods.setGoodsId(null);
        goods.setGoodsKeywords(goods.getGoodsName());
        goods.setGoodsTypeId(57);
        Integer random = (int)(10.0D + Math.random() * 990.0D);
        goods.setBrandId(1);
        goods.setGoodsStock(random);
        goods.setSaleCount(random);
        goods.setIsBook(0);
        goods.setBookQuantity(99);
        goods.setWarnStock(9);
        goods.setIsSale(1);
        goods.setSaleTime(new Date());
        Integer isRandom = (int)Math.random();
        goods.setIsBest(isRandom);
        goods.setIsHot(isRandom);
        goods.setIsNew(isRandom);
        goods.setIsAdminRecom(isRandom);
        goods.setRecommDesc("暂无");
        goods.setGoodsFlag(1);
        goods.setCreateTime(new Date());
        goodsDao.add(goods);
        return false;
    }

    @Override
    public PageBean<Goods> searchGoods(String key,String orderBy, Integer pageNo, Integer pageSize) {
        PageBean<Goods> pageBean = null;
        //获取总记录数
        Integer count = goodsDao.getCount(key);
        //查询结果
        List<Goods> list = goodsDao.getGoodsBySearchKey(key,orderBy,pageNo, pageSize);
        //将结果放到分页工具类
        pageBean = new PageBean<>(list,count,pageNo,pageSize);
        return pageBean;
    }

    @Override
    public List<Goods> getGoodsByLike(String key) {
        return goodsDao.getGoodsByLike(key);
    }

    @Override
    public List<Goods> getAll() {
        return goodsDao.getAll();
    }

    @Override
    public Goods getGoodsBySn(String goodSn) {
        return goodsDao.getGoodsBySn(goodSn);
    }
    /**
     * 根据编号查询商品
     * @param goodsId
     * @return
     */
    @Override
    public Goods getGoodsByGoodsId(Integer goodsId) {
        return goodsDao.getGoodsByGoodsId(goodsId);
    }

    /**
     * 根据编号和名字查询
     * @param id
     * @param name
     * @return
     */
    @Override
    public Goods queryOne(String id, String name, String type) {
        Goods goods = goodsDao.queryOne(id, name,type);
        return goods;
    }
    /**
     * 根据编号和名字查询
     * @param id
     * @param name
     * @return
     */
    @Override
    public List<Goods> queryOne(String id, String name) {
        List<Goods> goods = goodsDao.queryOne(id, name);
        return goods;
    }

    /**
     * 根据编号查询商品
     * @param id
     * @return
     */
    @Override
    public Goods queryById(String id) {
        Goods goods = goodsDao.queryById(id);
        return goods;
    }
    /**
     * 根据名称查询商品
     * @param name
     * @return
     */
    @Override
    public Goods queryByName(String name) {
        Goods goods = goodsDao.queryByName(name);
        return goods;
    }
    /**
     * 根据商品类型查询商品信息
     * @return
     */
    @Override
    public List<Goods> queryByGoodsType(String type) {
        List<Goods> goods = goodsDao.queryByGoodsType(type);
        return goods;
    }
    /**
     * 修改商品信息
     * @param goods
     * @return boolean
     */
    @Override
    public boolean update(Goods goods) {
        boolean update = goodsDao.update(goods);
        return update;
    }
    /**
     * 根据id修改isSale(下架)
     * @param goodsId
     * @return
     */
    @Override
    public boolean soldOut(Integer goodsId) {
        boolean b = goodsDao.soldOut(goodsId);
        return b;
    }
}
